﻿package com.atn.edamame.tools 
{
	import flash.events.*;
    import flash.display.*;
	import flash.events.*;
    import flash.display.*;
	import flash.text.*;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import com.atn.edamame.*;

	public class Filter extends EdamameControl
	{
		[Embed(source='../../../../pipe.png')]
		private var EmbedImage:Class;
		private var dlabel:Label;
		private var valueInput:TextInput;
		public var value:String;
		public override function collide(input: EdamameObject):void
		{
			if (input is EdamameString) {
				if (match(input)) {
					var b:EdamameString = input as EdamameString;
					var v:int = b.vy;
					var c:EdamameString = new EdamameString();
					edamameParent.addEdamameObject(c);
					c.x = this.x;
					c.y = this.y + this.height + 5;
					c.vy = 2;
					c.value = b.value;
				}
				input.destroy();
			}
		}

		public override function exec(variable:Object, onFinish:Function):void
		{
			var filterdArray:Array = new Array();
			var v:String;
			trace(value);
			for each(v in variable.value) {
				if (v.indexOf(value) != -1) {
					filterdArray.push(v);
				}
			}
			variable.value = filterdArray;
			onFinish({result:"ok"});
		}
		
		public function match(input: EdamameObject) :Boolean{
				var b:EdamameString = input as EdamameString;
				return (b.value.indexOf(valueInput.text) != -1);
		}
		
        public override function init():void {
			dlabel = new Label();
            dlabel.text = "string filter";
			dlabel.width = 150;
			dlabel.height = 20;
            addChild(dlabel);
			
			valueInput = new TextInput();
			valueInput.text = "baseball";
			valueInput.width = 200;
			valueInput.height = 20;
			value = valueInput.text;
			valueInput.addEventListener(Event.CHANGE, 
				function ():void { 
					value = valueInput.text;
				} 
			);
            addChild(valueInput);
			
			var ui:UIComponent = new UIComponent();
			var img:Bitmap = new EmbedImage() as Bitmap;
			ui.width = 100;
			ui.height = 30;
			ui.addChild(img);
			addChild(ui);
			dlabel.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			ui.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this.height = 100;
			//this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
	}	
	
}